package com.mcookie.app.minicompo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BlurMaskFilter.Blur;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class BrushEditerDialog extends Activity {

//	BrushSizeView brushSizeView;
	SeekBar bc_size_sk;
	RadioButton[] rb = new RadioButton[4];
	RadioGroup[] rg = new RadioGroup[2];
	String blurStyle = "normal";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.brush_control);
		
//		brushSizeView = new BrushSizeView(BrushEditerDialog.this);
		bc_size_sk = (SeekBar)findViewById(R.id.bc_size_sk);
		bc_size_sk.setMax(40);
		bc_size_sk.setProgress(MiniCompoActivity.BlurSize);
		SurfView.radius = MiniCompoActivity.BlurSize+5;
		
		
		bc_size_sk.setOnSeekBarChangeListener(seekbarCL);
		bc_size_sk.incrementProgressBy(1);
		
		ImageButton bc_cancel = (ImageButton)findViewById(R.id.bc_cancel);
		bc_cancel.setOnClickListener(onClic);
		
		ImageButton bc_ok = (ImageButton)findViewById(R.id.bc_ok);
		bc_ok.setOnClickListener(onClic);
		
		rb[0] = (RadioButton)findViewById(R.id.bc_erasebrush_rb);
		rb[1] = (RadioButton)findViewById(R.id.bc_unerasebrush_rb);
		rb[2] = (RadioButton)findViewById(R.id.bc_normal_rb);
		rb[3] = (RadioButton)findViewById(R.id.bc_feader_rb);

		
		rg[0] = (RadioGroup)findViewById(R.id.bc_rg1);
		rg[1] = (RadioGroup)findViewById(R.id.bc_rg2);
		
		for(int i = 0; i< rg.length ; i++){
			rg[i].setOnCheckedChangeListener(CheckCL);
		}
		
		if(MiniCompoActivity.BlurStyle != null){
			rb[3].setChecked(true);
			blurStyle = "normal";
			SurfView.Surfview_BlurStyle = Blur.NORMAL;
		} else {
			rb[2].setChecked(true);
			blurStyle = "null";
			SurfView.Surfview_BlurStyle = null;
		}
	}
	
	
	
	OnCheckedChangeListener CheckCL = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			
			if(group.getId() == R.id.bc_rg1){
				
			}
			
			if(group.getId() == R.id.bc_rg2){
				
				switch (checkedId) {
				
				
				case R.id.bc_normal_rb:
					blurStyle = "null";
					SurfView.Surfview_BlurStyle = null;
					break;
					
					
				case R.id.bc_feader_rb:
					blurStyle = "normal";
					SurfView.Surfview_BlurStyle = Blur.NORMAL;
					break;
					
					
				case R.id.bc_erasebrush_rb:
					
					break;
				case R.id.bc_unerasebrush_rb:
					
					break;
				}

			}
		}
	};
	
	/**
	 * �ش� ��ư�� ��Ŭ�� �����ʸ� ����!
	 */
	OnClickListener onClic = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.bc_cancel:
				returnValue(RESULT_CANCELED, 0, null, null);
				break;
				
			case R.id.bc_ok:
				int brushSize = bc_size_sk.getProgress();
				returnValue(RESULT_OK, brushSize, blurStyle, null);
				
				break;

			default:
				break;
			}
		}
	};
	
	/**
	 * �귯�� ũ�� ��ũ�� ������.
	 */
	OnSeekBarChangeListener seekbarCL = new OnSeekBarChangeListener() {
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			SurfView.radius = progress+5;
		}
	};
	
	/**
	 * 결과반환.
	 * @param resultCode ������п�
	 * @param brushSize �귯��ũ
	 * @param blurStyle �귯�� ��Ÿ
	 * @param erase �귯�� ���찳��
	 */
	private void returnValue(int resultCode, int brushSize, String blurStyle, String erase) {
		Intent intent = new Intent();
		
		if(resultCode == RESULT_OK){
			intent.putExtra("brushSize", brushSize);
			intent.putExtra("blurStyle", blurStyle);
			intent.putExtra("erase", erase);
			
			Log.d("june2", "�귯�� ���̾�α� ������ : " 
					+ "brushSize : " + brushSize + ", "
					+ "blurStyle : " + blurStyle + ", "
					+ "erase : " + erase);
			
			setResult(RESULT_OK, intent);
			
		} else {
			setResult(RESULT_CANCELED, intent);
		}
		
		finish();
	}
}
